package day13.io;
import java.io.*;
//测试  字节读写出
//总结:
//1,字节流写出的工具类: FileOutputStream / BufferedOutputStream(高级流/缓冲流)
//2,效率上来讲: BufferedOutputStream  >  FileOutputStream
//3,原因是: BufferedOutputStream 底层维护了一个byte[] buf用来缓冲数据,
// 当数组满了 就把数据一次性保存磁盘里 . 默认的数组容量是8192字节相当于8K
public class Test4_out {
    public static void main(String[] args) throws IOException {
//        method();// FileOutputStream写出
        method2();// BufferedOutputStream写出
    }
    // BufferedOutputStream写出
    public static void method2() throws IOException {
        //1,创建对象
//        BufferedOutputStream(OutputStream out)
        OutputStream out = new BufferedOutputStream (
                                new FileOutputStream("D:\\iotest\\2.txt") ) ;

        OutputStream out2 = new FileOutputStream("D:\\iotest\\2.txt");
        //2,开始写出
        out.write(48);
        out.write(49);
        out.write(50);
        //3,释放资源
//        out.flush();//把数据从out流里刷出去,刷到磁盘保存
        out.close();//flush + close
    }
    // FileOutputStream写出
    public static void method() throws IOException {
        //1,创建对象
//        FileOutputStream(File file)
//        OutputStream out = new FileOutputStream(new File(""));
//        FileOutputStream(String name)

//TODO 创建FileOutputStream对象时,只传1个参数,默认就是数据覆盖
// OutputStream out = new FileOutputStream("D:\\iotest\\1.txt");
 //TODO 想实现数据追加,就需要传入第二个参数true,表示要改成数据追加
 OutputStream out = new FileOutputStream("D:\\iotest\\2.txt",true);
        //2,开始写出
        out.write(97);
        out.write(97);
        out.write(97);
        out.write(97);
        //3,释放资源
        out.close();
        //TODO 流关了还能继续写出吗? --不可以,否则抛出异常:Stream Closed
//        out.write(100);
    }
}
